package com.example.lottery.enums;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wenhao.wang
 * @Description 前后端统一消息定义协议 Message  之后前后端数据交互都按照规定的类型进行交互
 */
public class Message<T> {

    // 消息头meta 存放状态信息 code message
    private Map<String,Object> meta = new HashMap<String,Object>();
    // 消息内容  存储实体交互数据
    private T data;
    //总数
    private Integer total;

    public Map<String, Object> getMeta() {
        return meta;
    }

    public Message<T> setMeta(Map<String, Object> meta) {
        this.meta = meta;
        return this;
    }

    public T getData() {
        return data;
    }

    public Message<T> setData(T data) {
        this.addMeta("success",Boolean.TRUE);
        this.addMeta("code",HttpCodeEnum.SUCCESS.getIndex());
        this.data = data;
        return this;
    }

    public Integer getTotal() {
        return total;
    }

    public Message<T> setTotal(Integer total) {
        this.total = total;
        return this;
    }

    public Message<T> addMeta(String key, Object object) {
        this.meta.put(key,object);
        return this;
    }

    public Message<T> ok() {
        this.addMeta("success",Boolean.TRUE);
        this.addMeta("code",HttpCodeEnum.SUCCESS.getIndex());
        this.addMeta("msg",HttpCodeEnum.SUCCESS.getName());
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        return this;
    }

    public Message<T> ok(HttpCodeEnum httpCodeEnum) {
        this.addMeta("success",Boolean.TRUE);
        this.addMeta("code",httpCodeEnum.getIndex());
        this.addMeta("msg",httpCodeEnum.getName());
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        return this;
    }

    public Message<T> ok(HttpCodeEnum httpCodeEnum,String message, String requestId) {
        this.addMeta("success",Boolean.TRUE);
        this.addMeta("code",httpCodeEnum.getIndex());
        this.addMeta("msg",message);
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        this.addMeta("requestId",requestId);
        return this;
    }

    public Message<T> ok(HttpCodeEnum httpCodeEnum,String message) {
        this.addMeta("success",Boolean.TRUE);
        this.addMeta("code",httpCodeEnum.getIndex());
        this.addMeta("msg",message);
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        return this;
    }

    public Message<T> error() {
        this.addMeta("success",Boolean.FALSE);
        this.addMeta("code",HttpCodeEnum.ERROR.getIndex());
        this.addMeta("msg",HttpCodeEnum.ERROR.getName());
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        return this;
    }

    public Message<T> error(HttpCodeEnum httpCodeEnum) {
        this.addMeta("success",Boolean.FALSE);
        this.addMeta("code",httpCodeEnum.getIndex());
        this.addMeta("msg",httpCodeEnum.getName());
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        return this;
    }

    public Message<T> error(HttpCodeEnum httpCodeEnum, String requestId) {
        this.addMeta("success",Boolean.FALSE);
        this.addMeta("code",httpCodeEnum.getIndex());
        this.addMeta("msg",httpCodeEnum.getName());
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        this.addMeta("requestId",requestId);
        return this;
    }

    public Message<T> error(Integer code, String msg) {
        this.addMeta("success",Boolean.FALSE);
        this.addMeta("code",code);
        this.addMeta("msg",msg);
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        return this;
    }

    public Message<T> error(Integer code, String msg,String requestId) {
        this.addMeta("success",Boolean.FALSE);
        this.addMeta("code",code);
        this.addMeta("msg",msg);
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        this.addMeta("requestId",requestId);
        return this;
    }

    public Message<T> myError(HttpCodeEnum httpCodeEnum, String requestId) {
        this.addMeta("success",Boolean.FALSE);
        this.addMeta("code",httpCodeEnum.getIndex());
        this.addMeta("msg",httpCodeEnum.getName());
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        this.addMeta("requestId",requestId);
        return this;
    }


    public Message<T> error(Integer code, Map<String,String> errorMsg, String requestId) {
        this.addMeta("success",Boolean.FALSE);
        this.addMeta("code",code);
        this.addMeta("msg",errorMsg);
        this.addMeta("timestamp",new Timestamp(new Date().getTime()));
        this.addMeta("requestId",requestId);
        return this;
    }

    @Override
    public String toString() {
        return "Message{" +
                "meta=" + meta +
                ", data=" + data +
                ", total=" + total +
                '}';
    }
}
